<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">调拨单列表</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="调拨单搜索" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入单据编码、公司名称、部门"
            clearable
            size="small"
            style="width: 250px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="按起止时间" prop="time">
          <el-date-picker
              v-model="time"
              type="daterange"
              range-separator="至"
              style="width: 230px;"
              @change="dataTime()"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="orderAdd()"
            v-hasPermi="['system:purchaseOrder:insert']"
          >新建</el-button>
        </el-col>
        <el-col :span="21">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="btnExport()"
          >导出</el-button>
        </el-col>
        <el-col :span="1">
          <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
            <el-checkbox-group v-model="checkedColumns" size="mini">
              <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
            </el-checkbox-group>
            <div title="筛选列" class="filter-table-col" slot="reference">
              <img src="@/assets/img/screening.png" width="25" />
            </div>
          </el-popover>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="list" @row-click="handleView" @selection-change="handleSelectionChange" height="calc(100vh - 350px)" border>
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="(item, index) in tableHeaderData">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :align="item.align"
            :sortable="item.sortable"
            :show-overflow-tooltip="true"
            :key="index"
            :width="item.width"
            v-if="colData[index].isTrue"
          >
            <template slot-scope="scope">
              <template v-if="item.label == '货品编码'">
                <div v-for="(item, index) in scope.row.detailList" :key="index" class="item">
                  {{ item.specificationCode }}
                </div>
              </template>
              <template v-else-if="item.label == '货品名称'">
                <div v-for="(item, index) in scope.row.detailList" :key="index" class="item">
                  {{ item.specificationName }}
                </div>
              </template>
              <template v-else-if="item.label == '单位'">
                  <div v-for="(item, index) in scope.row.detailList" :key="index">
                      {{ item.endNum }}
                  </div>
              </template>
              <template v-else-if="item.label == '调拨数量'">
                <div v-for="(item, index) in scope.row.detailList" :key="index" class="item">
                  {{ item.transferNum }}
                </div>
              </template>
              <template v-else-if="item.label == '状态'">
                <div v-if="scope.row.status == 0" style="color:#13C2C2">待提交</div>
                <div v-if="scope.row.status == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.status == 2" style="color:#52C41A">已完成</div>
                <div v-if="scope.row.status == 3" style="color:#fd562c">拒绝</div>
              </template>
              <span v-else>{{ scope.row[scope.column.property] }}</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              v-if="scope.row.status !== 2"
              @click.native.stop="handleDelete(scope.row)"
              v-hasPermi="['system:transferOrder:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { transferOrderList, transferOrderDel } from "@/api/system/transferOrder/transferOrder"
import { exportTransferOrder } from "@/api/system/export/export"
export default {
  name: "order",
  data() {
    return {
      loading: true,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: undefined,
        startDate: '',
        endDate: '',
      },
      time: [],
      ids: [],
      tableHeaderData: [
        { label: "单据编码", align: "center", prop: "code", width: "150"},
        { label: "调出公司名称", align: "center", prop: 'outCompanyName', width: "100" },
        { label: "调出部门", align: "center", prop: "outCompanyDept"},
        { label: "调出仓库名称", align: "center", prop: "outWarehouseName", width: "100"},
        { label: "调入公司名称", align: "center", prop: "inCompanyName", width: "100"},
        { label: "调入部门", align: "center", prop: "inCompanyDept" },
        { label: "调入仓库名称", align: "center", prop: "inWarehouseName", width: "100"},
        { label: "货品编码", align: "center", prop: "specificationCode", width: "130"},
        { label: "货品名称", align: "center", prop: "specificationName", width: "180"},
        { label: "单位", align: "center", prop: "unit"},
        { label: "调拨数量", align: "center", prop: "transferNum"},
        { label: "合计数量", align: "center", prop: "total"},
        { label: "状态", align: "center", prop: "status"},
      ],
      colData: [
        { title: "单据编码", isTrue: true },
        { title: "调出公司名称", isTrue: true },
        { title: "调出部门", isTrue: true },
        { title: "调出仓库名称", isTrue: true },
        { title: "调入公司名称", isTrue: true },
        { title: "调入部门", isTrue: true },
        { title: "调入仓库名称", isTrue: true },
        { title: "货品编码", isTrue: true },
        { title: "货品名称", isTrue: true },
        { title: "单位", isTrue: true },
        { title: "调拨数量", isTrue: true },
        { title: "合计数量", isTrue: true },
        { title: "状态", isTrue: true },
      ],
      // 当前选中的多选框，代表当前展示的列
      checkedColumns: ["单据编码", "调出公司名称", "调出部门", "调出仓库名称", "调入公司名称", "调入部门", "调入仓库名称", "货品编码", "货品名称", "单位", "调拨数量", "合计数量", "状态"],
      // 多选框的列表，列出表格的每一列
      checkBoxGroup: ["单据编码", "调出公司名称", "调出部门", "调出仓库名称", "调入公司名称", "调入部门", "调入仓库名称", "货品编码", "货品名称", "单位", "调拨数量", "合计数量", "状态"]
    }
  },
  created() {
    this.getList()
  },
  watch: {
    checkedColumns(val) {
      let arr = this.checkBoxGroup.filter(i => !val.includes(i));
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.isTrue = false;
        } else {
          i.isTrue = true;
        }
      });
      this.reload = Math.random()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      transferOrderList(this.addDateRange(this.queryParams)).then(res => {
          this.list = res.rows
          this.total = res.total
          this.loading = false
        }
      )
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.time = ''
      this.queryParams.startDate = ''
      this.queryParams.endDate = ''
      this.handleQuery()
    },
    // 新建
    orderAdd(){
      this.$router.push({path:"/warehouseManagement/warehouse/transferOrder/add"})
    },
    // 查看详情
    handleView(row) {
      if(row.status == '0' || row.status == '3'){
        this.$router.push({path:"/warehouseManagement/warehouse/transferOrder/edit", query:{id: row.id}})
      }else{
        this.$router.push({path:"/warehouseManagement/warehouse/transferOrder/detail", query:{id: row.id}})
      }
    },
    // 删除
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？').then(() => {
        transferOrderDel(row.id).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {})
    },
    // 时间选择器
    dataTime(){
      if(this.time){
        let day = 24 * 60 * 60 * 1000
        this.queryParams.startTime = this.parseTime(this.time[0])
        this.queryParams.endTime = this.parseTime(this.time[1].getTime() + day)
      }else if(this.time == null){
        this.queryParams.startDate = ''
        this.queryParams.endDate = ''
      }
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.map(item => item.id)
    },
    // 导出
    btnExport(){
      let a = this.colData.filter(i => {
        return i.isTrue == true
      })
      let b = []
      a.forEach((item) => {
        this.tableHeaderData.forEach((data) => {
          if(item.title == data.label){
            b.push({name: data.prop, value: item.title})
          }
        })
      })
      const obj = b.reduce((result, item) => {
        result[item.name] = item.value
        return result
      }, {})
      if(this.ids.length > 0){
        let data = {
          ids: this.ids,
          headMap: obj,
          searchValue: this.queryParams.searchValue,
          startDate: this.queryParams.startDate ? this.queryParams.startDate : undefined,
          endDate: this.queryParams.endDate ? this.queryParams.endDate : undefined
        }
        this.getExport(data)
      }else{
        let data = {
          headMap: obj,
          searchValue: this.queryParams.searchValue,
          startDate: this.queryParams.startDate ? this.queryParams.startDate : undefined,
          endDate: this.queryParams.endDate ? this.queryParams.endDate : undefined
        }
        this.getExport(data)
      }
    },
    getExport(data){
      this.$modal.loading("正在导出")
      exportTransferOrder(data).then(res => {
        this.$modal.closeLoading()
        console.log(res)
        let blob = new Blob([res], {
          type: res.type,
        })
        let downloadElement = document.createElement("a")
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = "调拨单.xls";
        document.body.appendChild(downloadElement)
        downloadElement.click()
        window.URL.revokeObjectURL(href)
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-table__body .el-table__cell{
  padding:0;
}
::v-deep .el-table__body .cell{
  padding: 10px;
}
.item{
  height: 25px;
  margin-bottom: 10px;
}
.item:last-child{
  margin-bottom: 0;
}
</style>